5600 #include "types.h"
5601 #include "x86.h"
5602 
5603 void*
5604 memset(void *dst, int c, uint n)
5605 {
5606   stosb(dst, c, n);
5607   return dst;
5608 }
5609 
5610 int
5611 memcmp(const void *v1, const void *v2, uint n)
5612 {
5613   const uchar *s1, *s2;
5614 
5615   s1 = v1;
5616   s2 = v2;
5617   while(n-- > 0){
5618     if(*s1 != *s2)
5619       return *s1 - *s2;
5620     s1++, s2++;
5621   }
5622 
5623   return 0;
5624 }
5625 
5626 void*
5627 memmove(void *dst, const void *src, uint n)
5628 {
5629   const char *s;
5630   char *d;
5631 
5632   s = src;
5633   d = dst;
5634   if(s < d && s + n > d){
5635     s += n;
5636     d += n;
5637     while(n-- > 0)
5638       *--d = *--s;
5639   } else
5640     while(n-- > 0)
5641       *d++ = *s++;
5642 
5643   return dst;
5644 }
5645 
5646 
5647 
5648 
5649 
5650 // memcpy exists to placate GCC.  Use memmove.
5651 void*
5652 memcpy(void *dst, const void *src, uint n)
5653 {
5654   return memmove(dst, src, n);
5655 }
5656 
5657 int
5658 strncmp(const char *p, const char *q, uint n)
5659 {
5660   while(n > 0 && *p && *p == *q)
5661     n--, p++, q++;
5662   if(n == 0)
5663     return 0;
5664   return (uchar)*p - (uchar)*q;
5665 }
5666 
5667 char*
5668 strncpy(char *s, const char *t, int n)
5669 {
5670   char *os;
5671 
5672   os = s;
5673   while(n-- > 0 && (*s++ = *t++) != 0)
5674     ;
5675   while(n-- > 0)
5676     *s++ = 0;
5677   return os;
5678 }
5679 
5680 // Like strncpy but guaranteed to NUL-terminate.
5681 char*
5682 safestrcpy(char *s, const char *t, int n)
5683 {
5684   char *os;
5685 
5686   os = s;
5687   if(n <= 0)
5688     return os;
5689   while(--n > 0 && (*s++ = *t++) != 0)
5690     ;
5691   *s = 0;
5692   return os;
5693 }
5694 
5695 
5696 
5697 
5698 
5699 
5700 int
5701 strlen(const char *s)
5702 {
5703   int n;
5704 
5705   for(n = 0; s[n]; n++)
5706     ;
5707   return n;
5708 }
5709 
5710 
5711 
5712 
5713 
5714 
5715 
5716 
5717 
5718 
5719 
5720 
5721 
5722 
5723 
5724 
5725 
5726 
5727 
5728 
5729 
5730 
5731 
5732 
5733 
5734 
5735 
5736 
5737 
5738 
5739 
5740 
5741 
5742 
5743 
5744 
5745 
5746 
5747 
5748 
5749 
